﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class Filter : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				UIApplication application = commandData.Application;
                Autodesk.Revit.ApplicationServices.Application application2 = application.Application;
				Document document = application.ActiveUIDocument.Document;
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				Settings settings = activeUIDocument.Document.Settings;
				Categories categories = settings.Categories;
				transaction = new Transaction(document, "软件设置");
				transaction.Start();
				Filter.listPipeTypeName = YJKElementFilter.GetElementNameByClass(typeof(PipingSystemType), document);
				Filter.listMechanicalTypeName = YJKElementFilter.GetElementNameByClass(typeof(MechanicalSystemType), document);
                List<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(Autodesk.Revit.DB.View), document);
				List<Element> elementByClass2 = YJKElementFilter.GetElementByClass(typeof(View3D), document);
				new FilteredElementCollector(document);
                System.Windows.Forms.Form form = new SystemSet(categories, elementByClass, elementByClass2, document);
				form.ShowDialog();
				transaction.Commit();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
                if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			return result;
		}

		private List<string> getPipeSystemTypeName(Document document)
		{
			List<string> list = new List<string>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
			ICollection<Element> collection = filteredElementCollector.OfClass(typeof(PipingSystemType)).ToElements();
			foreach (Element element in collection)
			{
				if (element != null && element is PipingSystemType)
				{
					list.Add(element.Name);
				}
			}
			return list;
		}

		public static List<string> listPipeTypeName = new List<string>();

		public static List<string> listMechanicalTypeName = new List<string>();
	}
}
